﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Threading;

namespace IsoStorageManagerLib
{
    public partial class IsoStorageManager
    {
        public static bool IsFileExists(string fileName)
        {
            if (String.IsNullOrEmpty(fileName))
                return false;

            try
            {
                using (var myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    // Check directory
                    string directoryName = Path.GetDirectoryName(fileName);
                    if (!string.IsNullOrEmpty(directoryName) && !myIsolatedStorage.DirectoryExists(directoryName))
                        return false;

                    // Check file
                    if (!myIsolatedStorage.FileExists(fileName))
                        return false;
                }
            }
            catch (Exception) { }

            return true;
        }
        
        public static bool DeleteFile(string fileName)
        {
            if (String.IsNullOrEmpty(fileName)) return false;

            try
            {
                using (var isoFiles = IsolatedStorageFile.GetUserStoreForApplication())
                    if (isoFiles.FileExists(fileName))
                        isoFiles.DeleteFile(fileName);
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }


        public static List<string> FindFiles(string searchPath)
        {
            if (String.IsNullOrEmpty(searchPath)) return null;

            try
            {
                using (var myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    // Check directory
                    string directoryName = Path.GetDirectoryName(searchPath);
                    if (!string.IsNullOrEmpty(directoryName) && !myIsolatedStorage.DirectoryExists(directoryName))
                        return new List<string>();

                    // Search files
                    string[] fileNames = myIsolatedStorage.GetFileNames(searchPath);
                    if (String.IsNullOrEmpty(directoryName))
                        directoryName = string.Empty;
                    return fileNames.Select(fileName => Path.Combine(directoryName, fileName)).ToList();
                }
            }
            catch (Exception)
            {
                return new List<string>();
            }
        }


        public static void DeleteDirectoryWithFiles(string dir)
        {
            if (String.IsNullOrEmpty(dir)) return;

            try
            {
                using (var isoFiles = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    foreach (var file in isoFiles.GetFileNames(dir + "\\*"))
                    {
                        var fileName = Path.Combine(dir, file);
                        if (isoFiles.FileExists(fileName))
                            isoFiles.DeleteFile(fileName);
                    }

                    foreach (var subdir in isoFiles.GetDirectoryNames(dir))
                    {
                        var dirName = Path.Combine(dir, subdir);
                        if (isoFiles.DirectoryExists(dirName))
                            DeleteDirectoryWithFiles(dirName);
                    }

                    var currentDirname = Path.GetDirectoryName(dir);
                    if (!String.IsNullOrEmpty(currentDirname) && isoFiles.DirectoryExists(currentDirname))
                        isoFiles.DeleteDirectory(currentDirname);
                }
            }
            catch (Exception)
            {
            }
        }

        public static bool AppendFile(String fileName, String content)
        {
            if (String.IsNullOrEmpty(fileName))
                return false;

            using (var fileMutex = new Mutex(false, String.Format("{0}@{1}", appUid, fileName)))
            {
                try
                {
                    fileMutex.WaitOne();
                    using (var myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        using (var writeFile = new StreamWriter(
                            new IsolatedStorageFileStream(fileName, FileMode.Append, myIsolatedStorage)))
                        {
                            writeFile.WriteLine(content);
                            writeFile.Close();
                        }
                    }
                }
                catch (Exception)
                {
                    return false;
                }
                finally
                {
                    fileMutex.ReleaseMutex();
                }
            }
            return true;
        }

    }
}
